import { defineStore } from 'pinia'
import { selectUserMenu } from '@/api/authApi.js'

export const useAuthStore = defineStore('auth', {
  state() {
    return {
      currentUser: null,
      token: null,
      menus: [],
      activeTitle: '',
    }
  },
  actions: {
    async initMenus() {
      let menus = await selectUserMenu()
      // 过滤掉已下线的“系统统计”等无效菜单
      this.menus = (menus || []).filter(
        (m) => !(
          (m.right_path && m.right_path.includes('/admin/stats')) ||
          (m.right_name && m.right_name.includes('系统统计'))
        ),
      )
    },
    clearStore() {
      this.currentUser = null
      this.token = null
      this.menus = []
    },
  },
  // 启用插件持久化 => 自动加载本地存储中的持久化数据
  persist: true,
})
